package {
	//import flashx.textLayout.elements.BreakElement;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.utils.*;

	/**
	 * @author lenovo
	 */
	public class Game extends Sprite {
		private var pez : Pez;
		
		private var paquete : Paquete;
		private var paquetes : Array = new Array();
		
		private var remolino : Remolino;
		private var remolinos : Array = new Array();
		
		private var coin: Coin;
		private var coins: Array = new Array();
		
		private var pressedUp : Boolean;
		private var pressedDown : Boolean;
		
		private var grados : int;
		private var i : int;
//		private var j :int;
		
		private var posX :int;
		private var posY : int;
		private var auxX : int;
		private var buxX : int;
		
		private var currY : int;
		private var currYpack : int;
		private var countAlive:int;
		private var currYcoin: int;
		
		private var aux : int;
		private var packCount : int;
		private var topeUp : Boolean;
		private var topeDown : Boolean;
		private var pez_alive : Boolean;
		private var pause : Boolean;
		private var par: Navegari;
		
		private var style : String;
//		private var type : String;
		
		public var toolbar : Toolbar = new Toolbar();
		private var countCoins : uint;
		private var countFloats : uint;

		public function Game(par:Navegari) : void {
			graphics.beginFill(0x00ffff);
			graphics.drawRect(0, 0, par.stage.stageWidth, par.stage.stageHeight);
			graphics.endFill();
					
			this.par = par;
			
			pez = new Pez();
			addChild(pez);
			pez.x = pez.width-pez.width/3;
			pez.y = par.stage.stageHeight / 2;
            
			posX = par.stage.stageWidth;
			posY = par.stage.stageHeight;
			auxX = posX;
			grados = 5;
			buxX = par.stage.stageWidth;
			
			remolino = new Remolino();
			addChild(remolino);
			remolinos.push(remolino);
			remolino.x = posX;
			
			i = 0;
			packCount = 0;
			
			paquete = new Paquete();
			addChild(paquete);
			paquetes.push(paquete);
            paquete.x = auxX ;
			
			
//			createArray();
			style = String(Math.floor(Math.random()*3) + 1);
			coin = new Coin (style);
			addChild(coin);
			coins.push(coin);
//			type = String(Math.floor(Math.random()*3) + 1);
//			pickType(type);
			 			
			currY = posY/5;
			currYpack = posY/3;
			currYcoin=posY /4;
			
			topeUp = false;
			topeDown = false;
			
			pez_alive = true;
			pause = true;
			
			countAlive = 30;
			countFloats = 0;
			
			par.stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
			par.stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			par.stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}

		private function onKeyDown(event : KeyboardEvent) : void {
			switch (event.keyCode) {
				case Keyboard.UP:
					pressedUp = true;
					break;
				case Keyboard.DOWN:
					pressedDown = true;
					break;
				case Keyboard.P:
					if (pause) pause=false;
					else pause=true;
					break;
			}
		}

		private function onKeyUp(event : KeyboardEvent) : void {
			switch (event.keyCode) {
				case Keyboard.UP:
					pressedUp = false;
					break;
				case Keyboard.DOWN:
					pressedDown = false;
					break;
			}
		}

		private function pezhits():void {
			pez_alive = true;
			addChild(pez);
			pez.x = pez.width-pez.width/3;
			pez.y = par.stage.stageHeight / 2 - pez.height / 2;
			pez.rotation = 0;
			countAlive = 30;
		}
		
		private function paquetehits():void{
			
			removeChild(paquetes[i]);
			paquetes.splice(i,1);		
			i = 0;
			paquete = new Paquete();
			auxX = par.stage.stageWidth + paquete.width;
			addChild(paquete);
			paquetes.push(paquete);
			paquetes[i].x = auxX - paquete.speed * Math.cos(grados*Math.PI/180);
			currYpack= Math.random()*par.stage.stageHeight - paquetes[i].speed * Math.sin(grados) / 2;
			if ((currY - paquete.height<0) || (currYpack + paquete.height>par.stage.stageHeight)) {
				while ((currYpack - paquete.height<0) || (currYpack + paquete.height>par.stage.stageHeight)) { 
					currYpack= Math.random()*par.stage.stageHeight - paquetes[i].speed * Math.sin(grados) / 2;				
				}
			}			
			paquetes[i].y = currYpack;
		}
		
		private function coinhits(): void{
			removeChild(coins[i]);
			coins.splice(i);
			i = 0;
			style = String(Math.floor(Math.random()*3)+1);
			coin = new Coin(style);
			buxX = stage.stageWidth + coin.width;
			addChild(coin);
			coins.push(coin);
			coins[i].x = buxX;
			currYcoin= Math.random()*stage.stageHeight - coins[i].speed * Math.sin(grados) / 2;
			if ((currY - coin.height<0) || (currYcoin + coin.height>stage.stageHeight)) {
				while ((currYcoin - coin.height<0) || (currYcoin + coin.height>stage.stageHeight)) {
					currYcoin= Math.random()*stage.stageHeight - coins[i].speed * Math.sin(grados) / 2;
				}
			}
			coins[i].y = currYcoin;
		}
		
//		private function createArray(): void{
//			style = String(Math.floor(Math.random()*3) + 1);
//			coin = new Coin (style);
//			coins.push(coin);
//			type = String(Math.floor(Math.random()*3) + 1);
//			pickType(type);
//			drawArr();
//			
//			buxX = stage.stageWidth;
//			coins[i][j].x = buxX;
//			currYcoin= Math.random()*stage.stageHeight - coin.speed * Math.sin(grados) / 2;
//			if ((currY - coin.height<0) || (currYcoin + coin.height>stage.stageHeight)) {
//				while ((currYcoin - coin.height<0) || (currYcoin + coin.height>stage.stageHeight)) { 
//					currYcoin= Math.random()*stage.stageHeight - coin.speed * Math.sin(grados) / 2;				
//				}
//			}			
//			coins[i].y = currYcoin;
//		}
		
//		private function destroyArray(): void{
//			removeChild(coins[i][j]);
//			coins.splice(i,j);
//		}
		
		private function pezalive(): void{
			pez.rotation = 0;
			if (pressedDown && topeUp){
				topeUp = false;
			}
			if (pressedUp && topeDown){
				topeDown = false;
			}
			if (pressedUp && !topeUp) {
				pez.rotation = -15;
				aux  = 15;
				pez.y -= 4*pez.speed * Math.sin(aux * Math.PI / 180);
				topeUp = pez.y<0 + pez.height/3;
			}
			if (pressedDown && !topeDown) {
				pez.rotation = 15;
				aux  = 15;
				pez.y += 4*pez.speed * Math.sin(aux * Math.PI / 180);
				topeDown = pez.y>par.stage.stageHeight - pez.height/3;
			}
			if (pressedUp && topeUp) {
				pez.rotation = - 15;
				pez.y = 0 + pez.height/3;
				topeUp = pressedDown;
			}
			if (pressedDown && topeDown) {
				pez.rotation = 15;
				pez.y = par.stage.stageHeight - pez.height/3;
				topeDown = pressedUp;
			}
			if (pressedDown && pressedUp){
				pez.rotation = 0;
			}
			if (pez.hitTestObject(paquetes[i])) {
				countFloats += 100;	
				paquetehits();
			}
			if (pez.hitTestObject(remolino)) {
				// pez.y=-stage.stageHeight-20;
				pez_alive = false;
				removeChild(pez);
				//var intervalId:uint = setTimeout(pezhits, 900);	
			}
			if (pez.hitTestObject(coin)){
				switch(style){
					case "1": 
						countCoins += 10;
					case "2": 
						countCoins += 5;
					case "3": 
						countCoins += 1;
				}
				toolbar.coins.text = 'Monedas: ' + countCoins.toString();
				coinhits();
			}
		}
		
		private function remolinoend(): void {
			removeChild(remolinos[i]);				
			remolinos.splice(i,1);
			i = 0;
			remolino = new Remolino();
			addChild(remolino);
			remolinos.push(remolino);
			
			posX = par.stage.stageWidth + remolinos[i].speed * Math.cos(grados) / 2;
			remolinos[i].x = posX  - remolinos[i].speed * Math.cos(grados) / 2;
			currY= Math.random()*par.stage.stageHeight - remolinos[i].speed * Math.sin(grados) / 2;
			if ((currY - remolino.height <0) || (currY + remolino.height>par.stage.stageHeight)){
				while ((currY - remolino.height<0) || (currY + remolino.height>par.stage.stageHeight)) { 
					currY= Math.random()*par.stage.stageHeight - remolinos[i].speed * Math.sin(grados) / 2 ;
				}
			}
			remolinos[i].y = currY;
		}
		
		private function onEnterFrame(e : Event) : void {
			if (pause){
				grados++;
				remolino.x = posX  - remolino.speed * Math.cos(grados) / 2;
				posX -= 10;
				remolino.y = currY - remolino.speed * Math.sin(grados) / 2;
				
				paquete.x = auxX - paquete.speed * Math.cos(grados*Math.PI/180);
				auxX -= 6;
				paquete.y = currYpack;
				
				coin.x = buxX;
				buxX-=6;
				coin.y = currYcoin;
				
				if (pez_alive){
					pezalive();
//					for (var i:uint = 0; i < coins.length ; i++) {
//						for (var j:uint = 0; j < (coins[i] as Array).length; j++) {
//							if (pez.hitTestObject(coins[i][j])){
//								countCoins ++;
//								toolbar.coins.text = 'Monedas: ' + countCoins.toString(); 
//								removeChild(coins[i][j]);				
//								coins.splice(i,j);
//							} else {
//								coins[i][j].x = coins[i][j].x - coin.speed;								
//							}
//							buxX = buxX - coin.speed;
//						}
//					}
				} else{
					countAlive--;
				}
				if (countAlive==0){
					pezhits();
				} 	
				if (grados == 20) {
					grados = 0;
				}
				if (posX<-remolino.width){
					remolinoend();	
				}
				if (auxX <-paquete.width){
					paquetehits();
				}	
				if (buxX<-coin.width){
					coinhits();
//					destroyArray();
//					createArray();
				}
			}
		}	
		 public function get packs():int{
            return countFloats;        
         }
         public function set packs(value:int):void{
            countFloats = value;
         }
		 public function get money():int{
            return countCoins;        
         }
         public function set money(value:int):void{
            countCoins = value;
         }		 	
	}
}
